package org.sun.web.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class SortMap implements Comparator{
	
	public void toMap(Object...vt){
		Map<Object,Object> data=new HashMap<Object, Object>();
		for(int i=0,size=vt.length;i<size;i++){
			data.put(vt[i], vt[i+1]);
		}
	}

	
	public int compare(Object o1, Object o2) {
		Map<String,Object> item1=(Map<String,Object>)o1;
		Map<String,Object> item2=(Map<String,Object>)o2;
		int x1=Integer.parseInt(item1.get("x").toString());
		int x2=Integer.parseInt(item2.get("x").toString());
		int y1=Integer.parseInt(item1.get("y").toString());
		int y2=Integer.parseInt(item2.get("y").toString());
		if(x1<x2){
			return -1;
		}else if(x1==x2){
			if(y1<y2) return -1;
			else return 1;
		}else{
			return 1;
		}
	}
	@Test
	public void test01(){
		List<Map<String,Object>> datas =new ArrayList<Map<String,Object>>();
		Map<String,Object> data1=new HashMap<String, Object>();
		data1.put("x", 200);
		data1.put("y", 200);
		datas.add(data1);
		Map<String,Object> data2=new HashMap<String, Object>();
		data2.put("x", 100);
		data2.put("y", 300);
		datas.add(data2);
		Map<String,Object> data3=new HashMap<String, Object>();
		data3.put("x", 30);
		data3.put("y", 500);
		datas.add(data3);
		Map<String,Object> data4=new HashMap<String, Object>();
		data4.put("x", 30);
		data4.put("y", 100);
		datas.add(data4);
		Collections.sort(datas, new SortMap());
		for(Map<String,Object> data :datas){
			System.out.println(data.get("x")+" , "+data.get("y"));
		}
	}
}
